﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;

namespace CanSWAT.ArcSWAT
{
    /// <summary>
    /// SWAT2009.mdb or SWAT2012.mdb, table crop and urban to get all landuses
    /// </summary>
    class ArcSWATDatabase : PersonalDatabase
    {
        private static string TABLE_NAME_CROP = "crop";
        private static string TABLE_NAME_URBAN = "urban";
        private static string TABLE_NAME_SOIL_USER = "usersoil";

        private static string FIELD_NAME_CROP_CODE = "CPNM";
        private static string FIELD_NAME_CROP_NAME = "CROPNAME";
        private static string FIELD_NAME_CROP_CLASSIFICATION = "IDC";

        private static string FIELD_NAME_URBAN_CODE = "URBNAME";
        private static string FIELD_NAME_URBAN_NAME = "URBFLNM";

        private ITable _cropTable = null;
        private ITable _urbanTable = null;
        private ITable _userSoilTable = null;
        private SortedDictionary<string, LanduseSnowHoldingCapacity> _defaultSnowHoldingCapacities = null;
        private Project _parentProject = null;

        public ArcSWATDatabase(string file,Project prj) : base(file)
        {
            _parentProject = prj;
        }

        private ITable UserSoilTable
        {
            get
            {
                if (_userSoilTable == null)
                    _userSoilTable = openTable(TABLE_NAME_SOIL_USER);
                return _userSoilTable;
            }
        }

        private ITable CropTable
        {
            get
            {
                if (_cropTable == null)
                    _cropTable = openTable(TABLE_NAME_CROP);
                return _cropTable;
            }
        }

        private ITable UrbanTable
        {
            get
            {
                if (_urbanTable == null)
                    _urbanTable = openTable(TABLE_NAME_URBAN);
                return _urbanTable;
            }
        }

        public SortedDictionary<string, LanduseSnowHoldingCapacity> DefaultSnowHoldingCapacities
        {
            get
            {
                if (_defaultSnowHoldingCapacities == null)
                {
                    _defaultSnowHoldingCapacities = new SortedDictionary<string, LanduseSnowHoldingCapacity>();

                    //read all crops, including 
                    ICursor cursor = CropTable.Search(null, false);

                    int nameIndex = CropTable.Fields.FindField(FIELD_NAME_CROP_NAME);
                    int codeIndex = CropTable.Fields.FindField(FIELD_NAME_CROP_CODE);
                    int classIndex = CropTable.Fields.FindField(FIELD_NAME_CROP_CLASSIFICATION);

                    IRow r = cursor.NextRow();
                    while (r != null)
                    {
                        string name = r.get_Value(nameIndex).ToString();
                        string code = r.get_Value(codeIndex).ToString();
                        int classs = Convert.ToInt32(r.get_Value(classIndex).ToString());

                        _defaultSnowHoldingCapacities.Add(code,
                            new LanduseSnowHoldingCapacity(name,code,classs));

                        r = cursor.NextRow();
                    }
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor);

                    //read all urban
                    cursor = UrbanTable.Search(null, false);
                    nameIndex = UrbanTable.Fields.FindField(FIELD_NAME_URBAN_NAME);
                    codeIndex = UrbanTable.Fields.FindField(FIELD_NAME_URBAN_CODE);

                    r = cursor.NextRow();
                    while (r != null)
                    {
                        string name = r.get_Value(nameIndex).ToString();
                        string code = r.get_Value(codeIndex).ToString();

                        _defaultSnowHoldingCapacities.Add(code,
                            new LanduseSnowHoldingCapacity(name, code, 0)); //classification of urban is set as 0

                        r = cursor.NextRow();
                    }
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor);
                }
                return _defaultSnowHoldingCapacities;
            }
        }
    }

    enum ArcSWATVersion
    {
        A2009,
        A2012
    }
}
